﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.Utils;
using DevExpress.LookAndFeel;
using System.Drawing;

namespace System
{
    /// <summary>
    /// Extensions for AppearanceObject
    /// </summary>
    public static class AppearanceObjectExt
    {
        public static event EventHandler ReadOnlyApperanceChanged;
        public static event EventHandler RequiredApperanceChanged;
        public static event EventHandler ErrorApperanceChanged;

        private static AppearanceObject defaultReadOnlyApperance;
        private static AppearanceObject defaultRequiredApperance;
        private static AppearanceObject defaultErrorAppearance;

        static UserLookAndFeel def = null;

        /// <summary>
        /// Adds UserLookAndFeel.Default.StyleChanged event handler
        /// </summary>
        public static void SetStyleEvents()
        {
            if (UserLookAndFeel.Default != null && UserLookAndFeel.Default != def)
            {
                if (def != null)
                {
                    def.StyleChanged -= new EventHandler(Default_StyleChanged);
                }
                UserLookAndFeel.Default.StyleChanged += new EventHandler(Default_StyleChanged);
            }
        }

        private static void Default_StyleChanged(object sender, EventArgs e)
        {
            Reset();
        }
        
        /// <summary>
        /// Gets the defaultReadOnlyAppearance
        /// </summary>
        public static AppearanceObject DefaultReadOnlyApperance
        {
            get
            {
                if (defaultReadOnlyApperance == null)
                {
                    
                    defaultReadOnlyApperance = new AppearanceObject("ReadOnlyApperance");
                    UserLookAndFeel defaultLF = UserLookAndFeel.Default;
                    if (defaultLF.ActiveStyle == ActiveLookAndFeelStyle.Skin)
                    {
                        DevExpress.Skins.Skin currentSkin;
                        DevExpress.Skins.SkinElement element;
                        string elementName;

                        currentSkin = DevExpress.Skins.CommonSkins.GetSkin(defaultLF);
                        elementName = DevExpress.Skins.CommonSkins.SkinTextBorder;
                        element = currentSkin[elementName];

                        AppearanceDefault defaultAppearance = new AppearanceDefault(element.GetForeColor(DevExpress.Utils.Drawing.ObjectState.Disabled),
                            element.Border.All);

                        element.Apply(defaultAppearance);
                        defaultReadOnlyApperance.Assign(defaultAppearance);
                        defaultReadOnlyApperance.ForeColor = element.GetForeColor(DevExpress.Utils.Drawing.ObjectState.Disabled);
                    }
                    else
                    {
                        defaultReadOnlyApperance.BackColor = SystemColors.Control;
                        defaultReadOnlyApperance.ForeColor = SystemColors.GrayText;
                        defaultReadOnlyApperance.BackColor2 = Color.Empty;
                    }
                    defaultReadOnlyApperance.Changed += new EventHandler(defaultReadOnlyApperance_Changed);
                    defaultReadOnlyApperance_Changed(defaultReadOnlyApperance, EventArgs.Empty);
                }
                return defaultReadOnlyApperance;
            }
        }

        static void defaultReadOnlyApperance_Changed(object sender, EventArgs e)
        {
            if (ReadOnlyApperanceChanged != null)
                ReadOnlyApperanceChanged(sender, EventArgs.Empty);
        }

        /// <summary>
        /// Gets default Requiered Appearance
        /// </summary>
        public static AppearanceObject DefaultRequiredApperance
        {
            get
            {
                if (defaultRequiredApperance == null)
                {
                    defaultRequiredApperance = new AppearanceObject("RequiredApperance");
                    defaultRequiredApperance.BackColor = SystemColors.Info;
                    defaultRequiredApperance.ForeColor = SystemColors.WindowText;
                    defaultRequiredApperance.BackColor2 = Color.Empty;
                    defaultRequiredApperance.Changed += new EventHandler(defaultRequiredApperance_Changed);
                    defaultRequiredApperance_Changed(defaultRequiredApperance, EventArgs.Empty);
                }
                return defaultRequiredApperance;
            }
        }

        static void defaultRequiredApperance_Changed(object sender, EventArgs e)
        {
            if (RequiredApperanceChanged != null)
                RequiredApperanceChanged(sender, EventArgs.Empty);
        }
        /// <summary>
        /// Gets default Error Appearance
        /// </summary>
        public static AppearanceObject DefaultErrorAppearance
        {
            get
            {
                if (defaultErrorAppearance == null)
                {
                    defaultErrorAppearance = new AppearanceObject("ErrorAppearance");
                    defaultErrorAppearance.BackColor = Color.Red;
                    defaultErrorAppearance.ForeColor = SystemColors.WindowText;
                    defaultErrorAppearance.BackColor2 = Color.Empty;
                    defaultErrorAppearance.Changed += new EventHandler(defaultErrorAppearance_Changed);
                    defaultErrorAppearance_Changed(defaultErrorAppearance, EventArgs.Empty);
                }
                return defaultErrorAppearance;
            }
        }

        static void defaultErrorAppearance_Changed(object sender, EventArgs e)
        {
            if (ErrorApperanceChanged != null)
                ErrorApperanceChanged(sender, e);
        }

        /// <summary>
        /// Sets default readonly appearance
        /// </summary>
        /// <param name="appearance"></param>
        /// <returns></returns>
        public static void SetReadOnlyAppearance(this AppearanceObject appearance)
        {
            appearance.Assign(DefaultReadOnlyApperance);
        }

        /// <summary>
        /// Sets default error appearance
        /// </summary>
        /// <param name="appearance"></param>
        /// <returns></returns>
        public static void SetErrorAppearance(this AppearanceObject appearance)
        {
            appearance.Assign(DefaultErrorAppearance);
        }

        /// <summary>
        /// Sets default required appearance
        /// </summary>
        /// <param name="appearance"></param>
        /// <returns></returns>
        public static void SetRequiredAppearance(this AppearanceObject appearance)
        {
            appearance.Assign(DefaultRequiredApperance);
        }
        
        /// <summary>
        /// Resets error, readonly and required appearance objects
        /// </summary>
        public static void Reset()
        {
            defaultErrorAppearance = null;
            defaultReadOnlyApperance = null;
            defaultRequiredApperance = null;
            var o = DefaultReadOnlyApperance;
            o = DefaultErrorAppearance;
            o = DefaultRequiredApperance;
        }

    }
}
